﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"Date.h"

Date::Date(int year, int month, int day)
{
	_year = year;
	_month = month;
	_day = day;

	if (!CheckDate())
	{
		cout << "非法日期:>";
		Print();
	}
}

bool Date::CheckDate()
{
	if (_year < 0 || _month < 1 || _month > 12 || _day < 1 || _day > GetMonthDay(_year, _month))
		return false;
	else
		return true;
}

void Date::Print() const
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

bool Date::operator<(const Date& d) const
{
	if (_year < d._year)
	{
		return true;
	}
	else
	{
		if (_year > d._year)
		{
			return false;
		}
		else
		{
			if (_month < d._month)
			{
				return true;
			}
			else
			{
				if (_month > d._month)
				{
					return false;
				}
				else
				{
					if (_day < d._day)
					{
						return true;
					}
					else
					{
						return false;
					}
				}
			}
		}
	}
}

bool Date::operator<=(const Date& d) const
{
	return (*this < d) || (*this == d);
}

bool Date::operator>(const Date& d) const
{
	return !(*this <= d);
}

bool Date::operator>=(const Date& d) const
{
	return !(*this < d);
}

bool Date::operator==(const Date& d) const
{
	return (_year == d._year) && (_month == d._month) && (_day == d._day);
}

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

// d1 += 天数
Date& Date::operator+=(int day)
{
	if (day < 0)
	{
		return *this -= -day;
	}

	_day += day;

	while (_day > GetMonthDay(_year, _month))
	{
		_day -= GetMonthDay(_year, _month);
		++_month;
		if (_month == 13)
		{
			_month = 1;
			++_year;
		}
	}

	return *this;
}

Date Date::operator+(int day) const
{
	Date tmp = *this;
	tmp += day;
	return tmp;
}

// d1 -= 天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		return *this += -day;
	}

	_day -= day;

	while (_day <= 0)
	{
		--_month;
		if (_month == 0)
		{
			_month = 12;
			--_year;
		}

		_day += GetMonthDay(_year, _month);
	}

	return *this;
}
Date Date::operator-(int day) const
{
	Date tmp = *this;
	tmp -= day;
	return tmp;
}

// ++d1 -> d1.operator++()
Date& Date::operator++()
{
	return *this += 1;
}

// d1++ -> d1.operator++(0)
// 为了区分，构成重载，给后置++，强⾏增加了⼀个int形参
// 这⾥不需要写形参名，因为接收值是多少不重要，也不需要⽤
// 这个参数仅仅是为了跟前置++构成重载区分
Date Date::operator++(int)
{
	Date tmp = *this;
	*this += 1;
	return tmp;
}

Date& Date::operator--()
{
	return *this -= 1;
}

Date Date::operator--(int)
{
	Date tmp = *this;
	*this -= 1;
	return tmp;
}

// d1 - d2
int Date::operator-(const Date& d) const
{
	int flag = 1;
	Date max = *this;
	Date min = d;
	if (max < min)
	{
		flag = -1;
		max = d;
		min = *this;
	}

	int count = 0;
	while (min != max)
	{
		++min;
		++count;
	}

	return count * flag;
}

// 重载
ostream& operator<<(ostream& out, const Date& d)
{
	out << d._year << "年" << d._month << "月" << d._day << "日";
	return out;
}

istream& operator>>(istream& in, Date& d)
{
	while (1)
	{
		cout << "请依次输入年月日:>";
		in >> d._year >> d._month >> d._day;

		if (!d.CheckDate())
		{
			cout << "非法输入!请重新输入" << endl;;
		}
		else
		{
			break;
		}
	}
	return in;
}